import requests
import allure
from config.enviroment import APIInfo
from utils.log.logger import log


class Base(object):
    #基础类，用于封装所有接口的方法
    def health(self):
        #检查健康接口
        try:
            response = requests.get(url = APIInfo.health)
            log.debug(f"向{APIInfo.health}发送了请求")
            if 200 <= response.status_code < 400:
                log.debug(f"访问成功，响应码为{response.status_code}")
            elif 400 <= response.status_code:
                log.error(f"访问失败，响应码为{response.status_code}")
                raise
            return response


        except requests.exceptions as error:
            log.error(f"遇到错误：{error}")
        except Exception as unknown_error:
            log.error(f"遇到未知错误{unknown_error}")

    def login(self, username, password):

        request_body = {
            "username": username,
            "password": password
        }
        try:

            response = requests.post(url = APIInfo.login,json = request_body)
            log.debug(f"向{APIInfo.login}发送了请求")


            if 200 <= response.status_code < 400:
                log.debug(f"访问成功，响应码为{response.status_code}")
            elif 400 <= response.status_code:
                log.error(f"访问失败，响应码为{response.status_code}")
                raise
            return response

        except requests.exceptions as error:
            log.error(f"遇到错误：{error}")
        except Exception as unknown_error:
            log.error(f"遇到未知错误{unknown_error}")



if __name__ == '__main__':
    print(Base().health())
    print(Base().login("weichuangxin", "xizhi123").text)